
guideUtil = guideUtil or {}

local s_maskimg = "common/zz.png"
local s_masktag = 100
local s_indtag = 101
local s_imgtag = 102

function guideUtil.invokeHitMask(callback, pos, indinfo, guideImgInfo)

    local mask = ccui.ImageView:create(s_maskimg, ccui.TextureResType.localType)
	local size = mask:getContentSize()
	local maskx = pos.x - size.width*0.5
    local masky = pos.y - size.height*0.5
    mask:setAnchorPoint({1.5,1.5})

    mask:setPosition(maskx, masky)
	local scene = gameUtil.curLuaScene
    scene.widget:addChild(mask, pp.z_guide_mask, s_masktag)
    uiEffect.FadeIn(mask, 0.2)

	d_print("init guide mask")
	mask:setTouchEnabled(false)

    local function onTouchBegan(touch, event)
        return true
    end

    local function onTouchEnded(touch, event)
        local location = touch:getLocation()
        
        if location.x > pos.x - 50 and
            location.x < pos.x + 50 and
            location.y > pos.y - 50 and
            location.y < pos.y + 50
        then
        	callback(location)
        end
    end

    local listener = cc.EventListenerTouchOneByOne:create()
    listener:setSwallowTouches(true)
    listener:registerScriptHandler(onTouchBegan,cc.Handler.EVENT_TOUCH_BEGAN )
    listener:registerScriptHandler(onTouchEnded,cc.Handler.EVENT_TOUCH_ENDED )
    local eventDispatcher = mask:getEventDispatcher()
    eventDispatcher:addEventListenerWithSceneGraphPriority(listener, mask)

    if indinfo then
	    local ind = uiIndicator2:create(indinfo.rot or -45)
	    ind:setPosition(indinfo.x, indinfo.y)
	    scene.widget:addChild(ind, pp.z_guide_mask+2, s_indtag)
	end

	if guideImgInfo then
		local guideImg = ccui.ImageView:create(guideImgInfo.img, ccui.TextureResType.localType)
		guideImg:setPosition(guideImgInfo.x, guideImgInfo.y)
		scene.widget:addChild(guideImg, pp.z_guide_mask+1, s_imgtag)
	end

   	scene:game_pause()
end

function guideUtil.removeHitMask()
    local scene = gameUtil.curLuaScene
    local mask = scene.widget:getChildByTag(s_masktag)
    --lkj doubt: need remove listener ?---eventDispatcher:removeEventListener(self.listener)
    uiEffect.FadeOut(mask, 0.2, true, true)

    local img = scene.widget:getChildByTag(s_imgtag)
    if img then
    	uiEffect.FadeOut(img, 0.2, true, true)
    end

    uiIndicator2:remove()
end


-------------------------------------------------------

guideCfg = guideCfg or {}

guideCfg.globeConst = {
	yuhengUnlockLevel = 2,
	fenjiUnlockLevel = 3,
}

guideCfg.callbacks = {
	touchToShoot = function(location)
		local scene = gameUtil.curLuaScene
		scene:game_resume_callback()
        scene:onGameViewTouched(location)
        scene:guide_itemFinish()
	end,

	shootYuheng = function(location)
		local scene = gameUtil.curLuaScene
		scene:game_resume_callback()
		scene:doYuhengCallback()
		scene:guide_itemFinish()
	end,

	shootFenji = function(location)
		local scene = gameUtil.curLuaScene
		scene:game_resume_callback()
		scene:doFenjiCallback()
		scene:guide_itemFinish()
	end
}

guideCfg.itemsOnBtReady = {
	[1] = {
	    desc = "teach1",
		condition = {
			level = 1,
		},
		onBegin = function()
			guideUtil.invokeHitMask(guideCfg.callbacks.touchToShoot, {x = 100, y = 410}, {x= 140, y = 380, rot = -45}) 
		end,
		onFinish = function()
            guideUtil.removeHitMask()
		end,
	},

	[2] = {
	   desc = "teach2",
        condition = {
            level = 1,
        },
		onBegin = function()
            guideUtil.invokeHitMask(guideCfg.callbacks.touchToShoot, {x = 230, y = 450}, {x = 270, y = 420, rot = -45}) 
		end,
        onFinish = function()
            guideUtil.removeHitMask()
		end,
	},

	[3] = {
	   desc = "teach3",
        condition = {
            level = 1,
        },
		onBegin = function()
            guideUtil.invokeHitMask(guideCfg.callbacks.touchToShoot, {x = 200, y = 450}, {x = 240, y = 420, rot = -45}) 
		end,
        onFinish = function()
            guideUtil.removeHitMask()
		end,
	},

	---------------------------------------------
	[4] = {
		desc = "teach_yuheng",
		condition = {
            level = guideCfg.globeConst.yuhengUnlockLevel,
            mapOffset = 100,
        },
		onBegin = function()
           guideUtil.invokeHitMask(guideCfg.callbacks.shootYuheng, {x = 443, y = 38}, {x = 403, y = 68, rot = -225}, {img = "warningPanel/yyyh.png", x = 100, y = 300}) 

           local scene = gameUtil.curLuaScene

            scene.bt_yuheng:setVisible(true)
            uiEffect.bounceIn(scene.bt_fenji)

            userData.weaponCnt_yuheng = userData.weaponCnt_yuheng + 2
            scene:refreshWeaponCnt()
		end,
        onFinish = function()
            guideUtil.removeHitMask()
		end,

	},

	[5] = {
		desc = "teach_fenji",
		condition = {
            level = guideCfg.globeConst.fenjiUnlockLevel,
            mapOffset = 100,
        },
		onBegin = function()
            guideUtil.invokeHitMask(guideCfg.callbacks.shootFenji, {x = 376, y = 38}, {x = 336, y = 68, rot = -225}, {img = "warningPanel/yyyh.png", x = 100, y = 300}) 

            local scene = gameUtil.curLuaScene

            scene.bt_fenji:setVisible(true)
            uiEffect.bounceIn(scene.bt_fenji)

            userData.weaponCnt_fenji = userData.weaponCnt_fenji + 2
            scene:refreshWeaponCnt()
		end,
        onFinish = function()
            guideUtil.removeHitMask()
		end,

	}
}

guideCfg.itemsOnMapOffset = {

	[1] = {
		desc = "key",
		condition = {
			level = 1,
			mapOffset = -10,
		},
		onBegin = function()
			local scene = gameUtil.curLuaScene
			WarningPanel.create(scene, "Guide_tip_key")
			scene:game_pause()
		end,
		onFinish = function()

		end

	},

	[2] = {
		desc = "wood",
		condition = {
			level = 2,
			mapOffset = 200,
		},
		onBegin = function()
			local scene = gameUtil.curLuaScene
			WarningPanel.create(scene, "Guide_tip_wood")
			scene:game_pause()
		end,
		onFinish = function()

		end

	},

	[3] = {
		desc = "boss",
		condition = {
			level = 5,
			mapOffset = -10
		},
		onBegin = function()
			local scene = gameUtil.curLuaScene
			WarningPanel.create(scene, "Guide_tip_boss")
			scene:game_pause()
		end,
		onFinish = function()

		end
	},
	
}


guideCfg.newbb = {
	wood = {
		level = 4,
		showRow = 20,
		delay = 2,
		warningPanelName = Newbb_Wood,
	},

	key = {
		level = 1,
		showRow = 1,
		delay = 2,
		warningPanelName = Newbb_Key,
	},	

	boss = {
		level = 5,
		showRow = 1,
		delay = 2,
		warningPanelName = Newbb_Boss,
	}	
}
